﻿///<reference path="../../../scripts/jquery-1.4.4.min.js" />
///<reference path="../../../scripts/jquery.cookie.js" />
///<reference path="jquery.treeview.js" />

$(function () {
    $("#Categories_Container").bind("dialogclose", function (e, ui) {
        //update selected
        displaySelectedCategories();
    });

    $("#close_categories_dialog").click(function () {
        $("#Categories_Container").dialog("close");
    });

    $("#Choose_Categories").click(function (e) {
        e.preventDefault();
        $("#Categories_Container").removeClass("hide").dialog({ modal: true, width: 700, maxHeight: 500 });
    });

});

$(document).ready(function () {
    var tree = $("#treeView_Categories").treeview({ collapsed: true });

    displaySelectedCategories();

    //limit to 1
    $("#treeView_Categories").find(":checkbox").bind("change", function () {
        var newValue = $(this).attr("checked");
        if (newValue) {
            var id = $(this).attr("id");
            $("#treeView_Categories").find(":checkbox").each(function (index, obj) {
                if ($(obj).attr("id") != id) {
                    $(obj).removeAttr("checked");
                }
            });
        }
    });
});


function displaySelectedCategories() {
    var selected = "";
    var selected_ids = "";
    $("#treeView_Categories").find("input:checked").each(function (index, obj) {
        if (selected.length > 0) {
            selected += ", ";
            selected_ids += ",";
        }
        selected += $(obj).attr("text");
        selected_ids += $(obj).attr("value");
    });

    if (selected.length == 0) {
        selected = "Not Selected";
    }
    $("#selected_categories").html(selected);
    $("#selected_Categories_Container").val(selected_ids);
}

